package JUC.fockjoin;

import org.junit.Test;

import java.util.OptionalLong;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

/**
 * @author mubei
 * @date 2021/11/22
 * @description
 */
public class TestForkJoin {


    @Test()
    public void test1() {
        long startTime = System.currentTimeMillis();
        long start = 0,end = 10_0000_0000,sum = 0L;
        for (long i = start; i <= end ; i++) {
            sum +=i;
        }
        System.out.println(sum);
        long endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
    }



    @Test()
    public void test2() throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        ForkJoinTask<Long> task = new ForkJoinDemo(0L, 10_0000_0000L);
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        //提交任务
       forkJoinPool.execute(task);


        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }


    @Test()
    public void test3() {
        long start = System.currentTimeMillis();
        Long aLong = LongStream.rangeClosed(0L, 10_0000_0000L).parallel().reduce(0,Long::sum);
        System.out.println(aLong);
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
